package com.axeiya.cheddargetter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

public class Invoice {
	
	private int number;
	
	private String type;
	
	private String vatRate;
	
	private Date billingDatetime;
	
	private String paidTransactionId;
	
	private Date createdDatetime;
	
	private ArrayList<Charge> charges;

	public Invoice(int number, String type, String vatRate, Date billingDatetime,
			String paidTransactionId, Date createdDatetime, ArrayList<Charge> charges) {
		super();
		this.number = number;
		this.type = type;
		this.vatRate = vatRate;
		this.billingDatetime = billingDatetime;
		this.paidTransactionId = paidTransactionId;
		this.createdDatetime = createdDatetime;
		this.charges = charges;
	}

	public static Invoice parseInvoice(Element invoice){
		
		int number = Integer.valueOf(invoice.getChildText("number"));
		String type = invoice.getChildText("type");
		String vatRate = invoice.getChildText("vatRate");
		Date billingDatetime = CGTools.parseDateTime(invoice.getChildText("billingDatetime"));
		String paidTransactionId = invoice.getChildText("paidTransactionId");
		Date createdDatetime = CGTools.parseDateTime(invoice.getChildText("createdDatetime"));
		
		ArrayList<Charge> charges = new ArrayList<Charge>();
		try {
			List<Element> chargesElement = XPath.selectNodes(invoice, "charges/charge");
			for (Element chargeElement : chargesElement) {
				charges.add(Charge.parseCharge(chargeElement));
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new Invoice(number, type, vatRate, billingDatetime, paidTransactionId, createdDatetime, charges);
		
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getVatRate() {
		return vatRate;
	}

	public void setVatRate(String vatRate) {
		this.vatRate = vatRate;
	}

	public Date getBillingDatetime() {
		return billingDatetime;
	}

	public void setBillingDatetime(Date billingDatetime) {
		this.billingDatetime = billingDatetime;
	}

	public String getPaidTransactionId() {
		return paidTransactionId;
	}

	public void setPaidTransactionId(String paidTransactionId) {
		this.paidTransactionId = paidTransactionId;
	}

	public Date getCreatedDatetime() {
		return createdDatetime;
	}

	public void setCreatedDatetime(Date createdDatetime) {
		this.createdDatetime = createdDatetime;
	}

	public ArrayList<Charge> getCharges() {
		return charges;
	}

	public void setCharges(ArrayList<Charge> charges) {
		this.charges = charges;
	}
	
	
}
